package com.wlstock.funi.ui;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.em.baselistener.OnBackListener;
import com.em.baselistener.OnMoreListener;
import com.em.widget.view.SelectPopUpWindow.onPopViewListener;
import com.wlstock.funi.R;
import com.wlstock.funi.adapter.SelfFundSetAdapter;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.dao.SelfFundSetDao;
import com.wlstock.funi.model.SelfFundSetModel;
import com.wlstock.funi.utils.IntentContants;
import com.wlstock.funi.utils.SharePreferenceUtils;
import com.wlstock.funi.widget.CaculateView;
import com.wlstock.funi.widget.CaculateView.OnValueClickListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监测的设置页面
 * @author admin
 *
 */
public class SelfFundSetActivity extends FundBaseFragmentActivity implements OnClickListener, OnMoreListener, onPopViewListener, OnBackListener, OnValueClickListener, SelfFundSetAdapter.onDeleteListener {
	
	private String stkCode;
	private List<SelfFundSetModel> stkSetList;
	private ListView lv_self_fund_set;
	private SelfFundSetAdapter setAdapter;
	private Button btn_add_set_mode;
	private Button btn_save_set_mode;
	private String stkName;
	
	private LinearLayout layout_top_caculate;
	private String open;
	private String high;
	private String low;
	private String cur;
	private String close;

	private int mode;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selffund_set);
		onMoreClick(this);
		onBackClick(this);
		
		layout_top_caculate = (LinearLayout) findViewById(R.id.layout_top_caculate);
		
		
		lv_self_fund_set = (ListView) findViewById(R.id.lv_self_fund_set);
		btn_add_set_mode = (Button) findViewById(R.id.btn_add_set_mode);
		btn_save_set_mode = (Button) findViewById(R.id.btn_save_set_mode);
		
		btn_add_set_mode.setOnClickListener(this);
		btn_save_set_mode.setOnClickListener(this);
		
		findViewById(R.id.btn_op_any).setOnClickListener(this);
		findViewById(R.id.btn_op_close).setOnClickListener(this);
		findViewById(R.id.btn_op_open).setOnClickListener(this);
		
		Intent intent = getIntent();
		if (intent!=null) {
			stkCode = intent.getStringExtra(IntentContants.INTENT_STKCODE);
			stkName = intent.getStringExtra(IntentContants.INTENT_STKNAME);
			open = intent.getStringExtra(IntentContants.INTENT_OPEN_PRICE);
			high = intent.getStringExtra(IntentContants.INTENT_HIGH_PRICE);
			low  = intent.getStringExtra(IntentContants.INTENT_LOW_PRICE);
			cur  = intent.getStringExtra(IntentContants.INTENT_CUR_PRICE);
			close = intent.getStringExtra(IntentContants.INTENT_LAST_CLOSE);
			mode = intent.getIntExtra(IntentContants.INTENT_MODE, IntentContants.INTENT_MODE_BUY);
		}
		addCaculatView();
		
		stkSetList = new ArrayList<SelfFundSetModel>();
		setAdapter = new SelfFundSetAdapter(this, stkSetList, R.layout.list_item_selffund_specific_set, mode);
		setAdapter.deleteListener = this;
		lv_self_fund_set.setAdapter(setAdapter);
		
		if (stkCode!=null&&stkName!=null) {
			setTitle(stkName);
			refreshList();
		}
	}

	private void addCaculatView() {
		CaculateView view0 = new CaculateView(this, CaculateView.VIEWMODE.UNEDITMODE);
		view0.initViewValues("昨收: ", this.close!=null?this.close:"", "-5");
		view0.valueClickListener = this;
		layout_top_caculate.addView(view0);

		CaculateView view1 = new CaculateView(this, CaculateView.VIEWMODE.UNEDITMODE);
		view1.initViewValues("开盘: ", this.open!=null?this.open:"", "0.5");
		view1.valueClickListener = this;
		layout_top_caculate.addView(view1);
		
		CaculateView view2 = new CaculateView(this, CaculateView.VIEWMODE.UNEDITMODE);
		view2.initViewValues("最高: ", this.high!=null?this.high:"", "-5");
		view2.valueClickListener = this;
		layout_top_caculate.addView(view2);
		
		CaculateView view3 = new CaculateView(this, CaculateView.VIEWMODE.UNEDITMODE);
		view3.initViewValues("最低: ", this.low!=null?this.low:"", "1");
		view3.valueClickListener = this;
		layout_top_caculate.addView(view3);
		
		CaculateView view4 = new CaculateView(this, CaculateView.VIEWMODE.UNEDITMODE);
		view4.initViewValues("现价: ", this.cur!=null?this.cur:"", "0");
		view4.valueClickListener = this;
		layout_top_caculate.addView(view4);

		CaculateView view5 = new CaculateView(this, CaculateView.VIEWMODE.UNEDITMODE);
		view5.initViewValues("任意: ", this.cur!=null?this.cur:"", "0");
		view5.valueClickListener = this;
		layout_top_caculate.addView(view5);
	}

	private void refreshList() {
		List<SelfFundSetModel> locResult = SelfFundSetDao.getInstance(this).querySetByStkCode(stkCode, SelfFundSetModel.PENDING);
		stkSetList.clear();
		if (locResult!=null && locResult.size()>0) {
			stkSetList.addAll(locResult);
			setAdapter.notifyDataSetChanged();
		}
		refreshLvHeight();;
	}
	private void refreshLvHeight() {
		ListAdapter adapter = lv_self_fund_set.getAdapter();
		if (adapter == null){
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < adapter.getCount(); i++) {
			View listItem = adapter.getView(i, null, lv_self_fund_set);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = lv_self_fund_set.getLayoutParams();
		params.height = totalHeight + (lv_self_fund_set.getDividerHeight() * (adapter.getCount()-1));
		lv_self_fund_set.setLayoutParams(params);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add_set_mode:
			SelfFundSetModel model = new SelfFundSetModel();
			model.setStkNo(stkCode);
			model.setStkName(stkName);
			Date curDate = new Date();
			model.setStartDay(curDate);
			model.setEndDay(curDate);
			model.setStartTime("09:30");
			model.setEndTime("10:30");
			model.setPending(SelfFundSetModel.PENDING);
			model.setTradeType(mode);
			model.setTotalMoney(10000);
			model.setQuickBuy(true);
			stkSetList.add(model);
			setAdapter.notifyDataSetChanged();
			break;
		case R.id.btn_save_set_mode:
			List<SelfFundSetModel> datas = setAdapter.getmDatas();
			SelfFundSetDao.getInstance(this).addOrUpdateList(datas);
			break;
		case R.id.btn_op_close:
			addCloseModel();
			break;
		case R.id.btn_op_open:
			addOpenModel();
			break;
		case R.id.btn_op_any:
			addAnyModel();
			break;
		default:
			break;
		}

		refreshLvHeight();

	}




	private void addAnyModel() {
		SelfFundSetModel model = new SelfFundSetModel();
		model.setStkNo(stkCode);
		model.setStkName(stkName);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		model.setStartDay(calendar.getTime());
		model.setEndDay(calendar.getTime());
		model.setStartTime("09:30");
		model.setEndTime("10:30");
		model.setPending(SelfFundSetModel.PENDING);
		model.setTradeType(mode);
		model.setPrice(SelfFundSetModel.ANY_PRICE);
		model.setTotalMoney((long)SharePreferenceUtils.getFloatSP(this, SharePrefConstants.DEFAULT_MONEY, 5000));
		model.setQuickBuy(true);
		stkSetList.add(model);
		setAdapter.notifyDataSetChanged();
		
	}

	private void addOpenModel() {
		SelfFundSetModel model = new SelfFundSetModel();
		model.setStkNo(stkCode);
		model.setStkName(stkName);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		model.setStartDay(calendar.getTime());
		model.setEndDay(calendar.getTime());
		model.setStartTime("09:30");
		model.setEndTime("09:31");
		model.setPending(SelfFundSetModel.PENDING);
		model.setTradeType(mode);
		model.setPrice(SelfFundSetModel.OPEN_PRICE);
		model.setTotalMoney((long)SharePreferenceUtils.getFloatSP(this, SharePrefConstants.DEFAULT_MONEY, 5000));
		model.setQuickBuy(true);
		stkSetList.add(model);
		setAdapter.notifyDataSetChanged();
	}

	private void addCloseModel() {
		SelfFundSetModel model = new SelfFundSetModel();
		model.setStkNo(stkCode);
		model.setStkName(stkName);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		model.setStartDay(calendar.getTime());
		model.setEndDay(calendar.getTime());
		model.setStartTime("14:57");
		model.setEndTime("14:59");
		model.setPending(SelfFundSetModel.PENDING);
		model.setPrice(SelfFundSetModel.CLOSE_PRICE);
		model.setTradeType(mode);
		model.setTotalMoney((long)SharePreferenceUtils.getFloatSP(this, SharePrefConstants.DEFAULT_MONEY, 5000));
		model.setQuickBuy(true);
		stkSetList.add(model);
		setAdapter.notifyDataSetChanged();
	}

	@Override
	public void onMoreClick(View v) {
		showPopUpView(getMenuList(), this);
	}

	private List<Map<String, Object>> getMenuList() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_valiable));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_unavaliable));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		return list;
	}

	@Override
	public void onViewSelectListener(String str) {
		if (str.equals(getResources().getString(R.string.pop_title_valiable))) {
			stkSetList.clear();
			stkSetList.addAll(SelfFundSetDao.getInstance(this).querySetByStkCode(stkCode, SelfFundSetModel.PENDING));
			setAdapter.notifyDataSetChanged();
		}else if (str.equals(getResources().getString(R.string.pop_title_unavaliable))) {
			stkSetList.clear();
			stkSetList.addAll(SelfFundSetDao.getInstance(this).querySetByStkCode(stkCode, SelfFundSetModel.DONE));
			setAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onBackClick(View v) {
		// TODO Auto-generated method stub
		this.finish();
	}

	@Override
	public void deliverValue(String value) {
		if (stkSetList!=null && stkSetList.size()>0 && value!=null && !value.isEmpty()) {
			SelfFundSetModel lastSetModel = stkSetList.get(stkSetList.size()-1);
			lastSetModel.setPrice(Float.valueOf(value));
			setAdapter.notifyDataSetInvalidated();
		}
	}


	@Override
	public void onDelete(SelfFundSetModel model) {
		refreshLvHeight();
	}
}
